package com.the4man.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

/**
 * Created by lenovo on 2017/12/12.
 */
public class Translater {
    private static final Logger logger = LoggerFactory.getLogger(Translater.class);
    public static Charset charset = Charset.forName("UTF-8");
    public static CharsetEncoder encoder;
    public static CharsetDecoder decoder;

    static {
        encoder = charset.newEncoder();
        decoder = charset.newDecoder();
    }

    public Translater() {
    }

    public static ByteBuffer toByteBuffer(String msg) {
        try {
            return encoder.encode(CharBuffer.wrap(msg));
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static String toString(ByteBuffer buffer) {
        String data = "";

        try {
            int old_position = buffer.position();
            data = decoder.decode(buffer).toString();
            buffer.position(old_position);
            return data;
        } catch (Exception var3) {
            logger.error("", var3);
            return "";
        }
    }
}
